package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.Update;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class warrpe {


    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testQueryWrapper() {
        // 查询姓名中包含“李”且薪资大于等于5000的员工的 id, name, phone, salary字段
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "李")
                .ge("salary", 5000)
                .select("id", "name", "phone", "salary");

        List<Emp> emps = empMapper.selectList(queryWrapper);
        emps.forEach(System.out::println);
    }

    @Test
    public void testUpdateByQueryWrapper() {
        // 更新名为"李忠"的员工的薪水为18000
        Emp emp = new Emp();
        emp.setSalary(18000);
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "宋江");
        empMapper.update(emp, queryWrapper);
    }

    //更新
    @Test
    public void testDeleteByQueryWrapper() {
      /*  更新id为5, 6, 7的员工的薪水，加2000，代码如下：
        update emp set salary = salary + 2000 where id in (5, 6, 7)    */
        UpdateWrapper<Emp> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", 5, 6, 7)
                .setSql("salary = salary + 2000");
        empMapper.update(null, updateWrapper);
    }

    @Test
    public void testDeleteByQueryWrapper1() {
        /* 使用 LambdaQueryWrapper*/
        LambdaUpdateWrapper<Emp> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Emp::getId, 5, 6, 7);
        lambdaUpdateWrapper.setSql("salary = salary + 2000");
        empMapper.update(null, lambdaUpdateWrapper);
    }

    @Test
    public void test2() {
    /*自定义sql*/

    }
}